package com.adb.androidwebalbum.ui;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

import com.adb.androidwebalbum.R;
import com.adb.androidwebalbum.bean.ImageBean;
import com.adb.androidwebalbum.uc.CustomBottomBar;
import com.adb.androidwebalbum.uc.CustomTopMenu;
import com.adb.androidwebalbum.uc.OnHeadNavigationSelectedListener;
import com.adb.androidwebalbum.utils.Const;
import com.adb.androidwebalbum.utils.Log;
import com.adb.androidwebalbum.utils.Utility;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

public class PhotosViewerActivity extends BaseActivity implements OnHeadNavigationSelectedListener,OnTouchListener{
	
	public final String TAG = PhotosViewerActivity.class.getCanonicalName();
	
	private CustomTopMenu mCustomTopMenu;
	private CustomBottomBar mCustomBottomBar;
	
	private ProgressDialog mProgressDialog;
	private int mPosition;
	private ImageView photo_view;
	
	private DisplayImageOptions mOptions;
	
	public ArrayList<ImageBean> arrListImageBean;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.debug(TAG,"[ onCreate ] ");
		Const.CONTEXT = PhotosViewerActivity.this;
		setContentView(R.layout.activity_photoviewer);
		
		arrListImageBean = new ArrayList<ImageBean>();
		for(ImageBean mBean : Utility.readObject(Const.FILE_PHOTOS_LIST)){
			arrListImageBean.add(mBean);
		}
		
		mPosition = Integer.parseInt(getIntent().getStringExtra(Const.INTENT_POSITION));
		Log.println("[ INDEX ] " + mPosition);
		photo_view = (ImageView)findViewById(R.id.photo_view);
		photo_view.setOnTouchListener(this);
		
		mCustomTopMenu = new CustomTopMenu(this,getLayoutInflater());
		mCustomBottomBar = new CustomBottomBar(this,getLayoutInflater());
		
		mOptions = new DisplayImageOptions.Builder()
		.showImageForEmptyUri(R.drawable.icon_menu_refresh)
		.showStubImage(R.drawable.icon_menu_refresh)
		.cacheInMemory()
		.cacheOnDisc()
		.build();
		
		
		
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		Log.debug(TAG,"[ onSaveInstanceState ] ");
		outState.putString(Const.INTENT_POSITION,mPosition+"");
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.debug(TAG,"[ onStart ] ");
		// show popup window when activity start 
		PerformMenu();
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		Log.debug(TAG,"[ onRestoreInstanceState ] ");
		mPosition = Integer.parseInt(savedInstanceState.getString(Const.INTENT_POSITION));
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		Log.debug(TAG,"[ onStop ] ");
	}
	@Override
	protected void onPause() {
		super.onPause();
		Log.debug(TAG,"[ onPause ] ");
	}
	@Override
	protected void onResume() {
		super.onResume();
		
		Log.debug(TAG,"[ onResume ] ");
		mProgressDialog = new ProgressDialog(PhotosViewerActivity.this);
		mProgressDialog.setMessage("Please wait....");
		mProgressDialog.setIndeterminate(true);
		mProgressDialog.setCancelable(false);
		
		runOnUiThread(new Runnable() {
			
			public void run() {
				ImageBean mImagePath = arrListImageBean.get(mPosition);
				Log.println("[ IMAGE ORDER ] " + mImagePath.getPhotoId());
				Log.println("[ IMAGE FULL PATH  ] " + mImagePath.getLarge_filepath());
				imageLoader.displayImage(mImagePath.getLarge_filepath(), photo_view,mOptions);
			}
		});
		
		//UPDATE TOP MENU
		mCustomBottomBar.setNavigationControl(mPosition,arrListImageBean);
		// UPDATE TOP MENU
		mCustomTopMenu.setPageSize(mPosition, arrListImageBean);
		
//		if (mPosition == 0) {
//			// btn_prv.setVisibility(View.GONE);
//			btn_prv.setBackgroundResource(R.drawable.btn_prv_disabled);
//			btn_prv.setEnabled(true);
//			btn_next.setBackgroundResource(R.drawable.btn_next_selector);
//			btn_next.setVisibility(View.VISIBLE);
//
//		} else if (mPosition > 0 && !(mPosition == arrListImageBean.size() - 1)) {
//			btn_next.setBackgroundResource(R.drawable.btn_next_selector);
//			btn_next.setVisibility(View.VISIBLE);
//			btn_prv.setBackgroundResource(R.drawable.btn_prv_selector);
//			btn_prv.setVisibility(View.VISIBLE);
//
//		} else if (mPosition > 0 && mPosition == arrListImageBean.size() - 1) {
//			// btn_next.setVisibility(View.GONE);
//			btn_next.setBackgroundResource(R.drawable.btn_next_disabled);
//			btn_next.setEnabled(true);
//			btn_prv.setBackgroundResource(R.drawable.btn_prv_selector);
//			btn_prv.setVisibility(View.VISIBLE);
//		}
		
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.debug(TAG,"[ onDestroy ] ");
		hidingMenu();
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	
		if (keyCode == KeyEvent.KEYCODE_MENU) {
	    	PerformMenu();
	    	return true; //always eat it!	    	
	    }
	    else if(keyCode == KeyEvent.KEYCODE_BACK)
	    {
	    	if (mCustomTopMenu.isShowing()) {
	    		mCustomTopMenu.hide();
	    		if(mCustomBottomBar.isShowing()){
		    		mCustomBottomBar.hide();
		    	}
	    		return true;
	    	}
	    	
	    }
		
		return super.onKeyDown(keyCode, event); 
	}
	
	private  void PerformMenu() {
		
		if(mCustomTopMenu.isShowing()){
			mCustomTopMenu.hide();
		}else{
			
			findViewById(R.id.main_layout).post(new Runnable() {
				   public void run() {
					   mCustomTopMenu.show(findViewById(R.id.main_layout),mPosition,arrListImageBean);
				   }
				});
			
		}
		
		if(mCustomBottomBar.isShowing()){
			mCustomBottomBar.hide();
		}else{
			
			findViewById(R.id.main_layout).post(new Runnable() {
				   public void run() {
					   mCustomBottomBar.show(findViewById(R.id.main_layout),mPosition,arrListImageBean);
				   }
				});
			
		}
		
	}
	private void hidingMenu(){

		if(mCustomTopMenu.isShowing()){
			mCustomTopMenu.hide();
		}
		if(mCustomBottomBar.isShowing()){
			mCustomBottomBar.hide();
		}
	}
	
	private Handler AppHandler = new Handler(){
		
		public void handleMessage(Message msg) {

			Log.println("[ INDEX ] " + mPosition);
			//UPDATE BOTTOM BAR
			mCustomBottomBar.setNavigationControl(mPosition, arrListImageBean);
			// UPDATE TOP MENU
			mCustomTopMenu.setPageSize(mPosition, arrListImageBean);
			switch (msg.what) {
			case 0:
				mProgressDialog.dismiss();
				break;
			case 1:
				mProgressDialog.dismiss();
				break;
			}
		}
	};

	public void onButtonSelected(int id) {
		switch (id) {
		
		case R.id.btn_next:
			
			if (arrListImageBean != null && mPosition < arrListImageBean.size() - 1) {
				mPosition++;
//				Toast.makeText(getApplicationContext(),mPosition+"",Toast.LENGTH_SHORT).show();
				mProgressDialog.show();
					new Thread(new Runnable() 
					{
						
						public void run() 
						{
							photo_view.post(new Runnable() {
								
								public void run() {
									
									Log.println("[ URL]" + arrListImageBean.get(mPosition).getLarge_filepath());
									imageLoader.displayImage(arrListImageBean.get(mPosition).getLarge_filepath(), photo_view,mOptions);
									
								}
							});
							AppHandler.sendMessage(AppHandler.obtainMessage(0));
						}

					}).start();

				}
			
			break;
			
		case R.id.btn_prv:
			
			if (arrListImageBean != null && mPosition > 0) {
				mPosition--;
//				Toast.makeText(getApplicationContext(),mPosition+"",Toast.LENGTH_SHORT).show();
				mProgressDialog.show();
					new Thread(new Runnable() {
						public void run() {
							
							photo_view.post(new Runnable() {
								
								public void run() {
									
									Log.println("[ URL ] " + arrListImageBean.get(mPosition).getLarge_filepath());
									imageLoader.displayImage(arrListImageBean.get(mPosition).getLarge_filepath(), photo_view,mOptions);
								}
							});
							AppHandler.sendMessage(AppHandler.obtainMessage(1));
						}

					}).start();
				}
			
			break;
		case R.id.btn_back:
			finish();
			break;
	

		}
	}
	public boolean onTouch(View v, MotionEvent event) {
		
		switch (event.getAction()) 
		{
			case MotionEvent.ACTION_UP:
				PerformMenu();
				break;
		}
		return true;
		
	}

	public void onTapListener(int id) {
		// TODO Auto-generated method stub
		
	}
	 
}
